const cloud = require('@cloudbase/node-sdk')
const app = cloud.init({
    env: cloud.SYMBOL_CURRENT_ENV
})
// 1. 获取数据库引用
const db = app.database();

exports.main = async (event) => {
    switch (event.action) {
        case 'editInformation':
        {
            return editInformation(event.data);
        }
        case 'editProject':
        {
            return editProject(event.data);
        }
        case 'editService':
        {
            return editService(event.data);
        }
    }
    return {
        event
    };
};

/**
 * 修改【公司资讯】数据
 * @param data
 */
const editInformation = async ({id}) => {
    const _ = db.command;
    if (id) {
        let res =  await db.collection("hc-news").where({_id: id})
            .update({
                readingNumber: _.inc(1)
            });
        return res;
    }
    return null;
}

/**
 * 修改【项目案例】
 * @param id
 * @returns {Promise<null|*>}
 */
const editProject = async ({id}) => {
    const _ = db.command;
    if (id) {
        let res =  await db.collection("hc-projects").where({_id: id})
            .update({
                readingNumber: _.inc(1)
            });
        return res;
    }
    return null;
}
/**
 * 修改【服务内容】
 * @param id
 * @returns {Promise<null|*>}
 */
const editService = async ({id}) => {
    const _ = db.command;
    if (id) {
        let res =  await db.collection("hc-services").where({_id: id})
            .update({
                readingNumber: _.inc(1)
            });
        return res;
    }
    return null;
}

